package com.lottery.im.module.contact.ui;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.lottery.im.R;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.chat.ui.UserProfileFragment;
import com.lottery.im.module.contact.adapter.BlackListAdapter;
import com.lottery.im.module.contact.presenter.BlackListPresenter;
import com.lottery.im.module.contact.view.BlackListView;
import com.lottery.im.module.tim.model.FriendProfile;
import com.lottery.im.util.StatusBarHelper;
import com.tencent.imsdk.TIMUserProfile;
import java.util.ArrayList;
import java.util.List;

/**
 * 黑名单列表
 */
public class BlackListFragment extends BaseSwipeFragment<BlackListPresenter>
    implements BlackListView {

  @BindView(R.id.tv_title) TextView mTvTitle;
  @BindView(R.id.tv_empty) TextView mTvEmpty;
  @BindView(R.id.list) RecyclerView mList;

  private BlackListAdapter mAdapter;
  private List<FriendProfile> mProfileList;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_blacklist;
  }

  @Override protected void initPresenter() {
    presenter = new BlackListPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    mTvTitle.setText("黑名单列表");
    mList.setLayoutManager(new LinearLayoutManager(getActivity()));
    RecyclerViewDivider.with(mActivity)
        .color(ContextCompat.getColor(mActivity, R.color.windowBackground))
        .size((int) mActivity.getResources().getDimension(R.dimen.line_size))
        .hideLastDivider()
        .build()
        .addTo(mList);
    mProfileList = new ArrayList<>();
    mAdapter = new BlackListAdapter(mProfileList);
    mAdapter.setOnItemClickListener((adapter, view, position) -> startFragmentForResult(UserProfileFragment.create(mProfileList.get(position).getIdentify(),1),888));
    mList.setAdapter(mAdapter);
    presenter.getBlackList();
  }

  @OnClick(R.id.iv_back) public void onViewClicked() {
    popBackStack();
  }

  @Override public void getListSuc(List<TIMUserProfile> timUserProfiles) {
    mProfileList.clear();
    if (null != timUserProfiles) {
      for (TIMUserProfile item : timUserProfiles) {
        FriendProfile profile = new FriendProfile(item);
        mProfileList.add(profile);
      }
    }
    mAdapter.notifyDataSetChanged();
    if (mProfileList.size() == 0) {
      mTvEmpty.setVisibility(View.VISIBLE);
      mList.setVisibility(View.GONE);
    } else {
      mList.setVisibility(View.VISIBLE);
      mTvEmpty.setVisibility(View.GONE);
    }
  }

  @Override public void onResume() {
    super.onResume();
    StatusBarHelper.setStatusBarDarkMode(getActivity());
  }

  @Override protected void onFragmentResult(int requestCode, int resultCode, Intent data) {
    super.onFragmentResult(requestCode, resultCode, data);

    if (requestCode == 888 && resultCode == RESULT_OK){
      presenter.getBlackList();
    }

  }
}
